package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.constants.ArticleConstants;
import com.heima.model.search.vos.SearchArticleVo;
import freemarker.ext.servlet.FreemarkerServlet;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.io.*;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.Map;

/**
 * @author rookie001
 * @creat 2023-06-12-15:06
 */
@Service
@Slf4j
@Transactional
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {
    @Autowired
    private Configuration configuration;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private ApArticleService apArticleService;

    // 生成静态文件上传到minIO中
    @Async//异步调用
    @Override
    public void buildArticleToMinIO(ApArticle apArticle, String content) {
        //数据校验
        if (StringUtils.isNotEmpty(content)) {
            //合成文件
            try {
                Map<String, Object> map = new HashMap<>();
                map.put("content", JSONArray.parseArray(content));//////序列化json
                //生成html文件
                Template template = configuration.getTemplate("article.ftl");
                StringWriter stringWriter = new StringWriter();
                template.process(map, stringWriter);
                //将文件上传到minio
                InputStream inputStream = new ByteArrayInputStream(stringWriter.toString().getBytes());
                //保存并返回路径
                String s = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", inputStream);
                //更新修改ap_article
                apArticle.setStaticUrl(s);
                apArticleService.update(apArticle, Wrappers.<ApArticle>lambdaUpdate().eq(ApArticle::getId, apArticle.getId()));
                //生成文章时同步保存到 es索引库中
                createArticleESIndex(apArticle,content,s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Autowired
    private KafkaTemplate kafkaTemplate;

    //发消息 //生成文章时同步保存到 es索引库中
    private void createArticleESIndex(ApArticle apArticle, String content, String path) {
        //创建SearchArticleVo
        SearchArticleVo searchArticleVo=new SearchArticleVo();
        BeanUtils.copyProperties(apArticle, searchArticleVo);
        searchArticleVo.setContent(content);
        searchArticleVo.setStaticUrl(path);
        kafkaTemplate.send(ArticleConstants.HOT_ARTICLE_FIRST_PAGE, JSON.toJSONString(searchArticleVo));
    }
}
